//
//  GDRemindTool.m
//  GuiderHealth
//
//  Created by mo on 2020/8/21.
//  Copyright © 2020 YM&EF. All rights reserved.
//

#import "GDRemindTool.h"
#import <UIKit/UIKit.h>
#import "BaseToolsKit.h"

@interface GDRemindTool ()

@property(nonatomic, strong) void(^remindToolClick)(BOOL  confirm);
//文本输入框内容
@property (nonatomic, strong) void(^relationShipBlock)(NSString *contentString);
@property (nonatomic, strong) UITextField *contentTextField;

@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *sureButton;

@end

@implementation GDRemindTool

+ (void)GDRemindToolTitle:(NSString *)title subtitle:(NSString *)subtitle ButtonClick:(void(^)(BOOL confirm))clickBlock{
    
    UIView *bgView = [KWINDOW createUIViewWithBackgroudColor:[[UIColor blackColor] colorWithAlphaComponent:0.7f]];
    bgView.frame = KWINDOW.bounds;
    
    UIView *contentView = [bgView createUIViewWithBackgroudColor:[UIColor whiteColor]];
    contentView.layer.cornerRadius = 10;
    contentView.layer.masksToBounds = YES;
    contentView.frame = CGRectMake(47, 3.2*SafeAreaNavBarHeight, bgView.width-94, 180);

    UILabel *titleLabel = [contentView createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:0 andTextColor:RGBFromHex(0x333333) andFont:Font16 andTextAlignment:NSTextAlignmentCenter andText:title];
    titleLabel.width = contentView.width-40;
    [titleLabel sizeToFit];
    titleLabel.y = 20;
    titleLabel.centerX = contentView.width / 2;

    UILabel *subLabel = [contentView createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:0 andTextColor:Color_51 andFont:Font14 andTextAlignment:NSTextAlignmentCenter andText:subtitle];
    subLabel.width = contentView.width-30;
    [subLabel sizeToFit];
    subLabel.y = titleLabel.maxY+15;
    subLabel.centerX = contentView.width / 2;

    CGFloat width = (contentView.width - 90) / 2.0;

    UIButton *cancelBtn = [GDRemindTool cancelButtonWithFrame:CGRectMake(30, subLabel.maxY+25, width, 44) superView:contentView];
    [cancelBtn addAcionBlock:^(UIButton * _Nonnull sender) {
        clickBlock(NO);
        [bgView removeFromSuperview];
    }];
    UIButton *sureButton = [GDRemindTool sureButtonButtonWithFrame:CGRectMake(cancelBtn.maxX+30, cancelBtn.y, cancelBtn.width, cancelBtn.height) superView:contentView];
    [sureButton addAcionBlock:^(UIButton * _Nonnull sender) {
        clickBlock(YES);
        [bgView removeFromSuperview];
    }];
    
    contentView.height = sureButton.maxY + 30;
}
///全局消息类型提示框
+ (void)GDMessageViewWithContentStr:(NSString *)content ButtonClick:(void(^)(BOOL confirm))clickBlock{
    UIView *bgView = [KWINDOW createUIViewWithBackgroudColor:[[UIColor blackColor] colorWithAlphaComponent:0.7f]];
    bgView.frame = KWINDOW.bounds;
    
    UIView *contentView = [bgView createUIViewWithBackgroudColor:[UIColor whiteColor]];
    contentView.layer.cornerRadius = 10;
    contentView.layer.masksToBounds = YES;
    contentView.size = CGSizeMake(282, 180);
    contentView.center = CGPointMake(bgView.width/2, KSCREEN_height/2-SafeAreaNavBarHeight/2);
    
    UILabel *titlelabel = [contentView createUILabelWithBackgroudColor:nil andNumberOfLines:1 andTextColor:MyTitleTextColor andFont:PingFang_SC_Light16 andTextAlignment:NSTextAlignmentLeft andText:GDBaselocalString(@"提示")];
    titlelabel.frame = CGRectMake(20, 0, 80, 44);
    
    UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(contentView.width-44, 0, 44, 44)];
    [deleteButton setImage:GDBaseImage(@"message_delete") forState:UIControlStateNormal];
    [contentView addSubview:deleteButton];
    [deleteButton addAcionBlock:^(UIButton * _Nonnull sender) {
        clickBlock(NO);
        [bgView removeFromSuperview];
    }];
    
    [contentView createRowLineViewWithBackgroudColor:myLineColor andX:0 andY:44 andWidth:contentView.width];
    
    UILabel *contentLabel = [contentView createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:0 andTextColor:MyTitleTextColor andFont:PingFang_SC_Light14 andTextAlignment:NSTextAlignmentCenter andText:content];
    contentLabel.width = contentView.width-40;
    [contentLabel sizeToFit];
    contentLabel.y = deleteButton.maxY + 15;
    contentLabel.centerX = contentView.width/2;
    
    UIButton *sureButton = [contentView createUIButtonWithBackgroudColor:MainColor andFont:PingFang_SC_Light15 andNormalTitleColor:UIColor.whiteColor andNormalImage:nil andNormalBackgroundImage:nil andCornerRadius:18 andAlignment:UIControlContentHorizontalAlignmentCenter andTitle:GDBaselocalString(@"查看")];
    sureButton.frame = CGRectMake(0, contentLabel.maxY+ 30, 124, 40);
    sureButton.centerX = contentView.width/2;
    [sureButton addAcionBlock:^(UIButton * _Nonnull sender) {
        clickBlock(YES);
        [bgView removeFromSuperview];
    }];
    
    contentView.height = sureButton.maxY + 30;
}
///全局弹窗提示框 无确认按钮
+ (void)GDRemindViewWithContentStr:(NSString *)content{
    
    UIView *bgView = [KWINDOW createUIViewWithBackgroudColor:[[UIColor blackColor] colorWithAlphaComponent:0.7f]];
    bgView.frame = KWINDOW.bounds;
    
    UIView *contentView = [bgView createUIViewWithBackgroudColor:[UIColor whiteColor]];
    contentView.layer.cornerRadius = 10;
    contentView.layer.masksToBounds = YES;
    contentView.size = CGSizeMake(282, 180);
    contentView.center = CGPointMake(bgView.width/2, KSCREEN_height/2-SafeAreaNavBarHeight/2);
    
    UILabel *titlelabel = [contentView createUILabelWithBackgroudColor:nil andNumberOfLines:1 andTextColor:MyTitleTextColor andFont:PingFang_SC_Light16 andTextAlignment:NSTextAlignmentLeft andText:GDBaselocalString(@"提示")];
    titlelabel.frame = CGRectMake(20, 0, 80, 44);
    
    UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(contentView.width-44, 0, 44, 44)];
    [deleteButton setImage:GDBaseImage(@"message_delete") forState:UIControlStateNormal];
    [contentView addSubview:deleteButton];
    [deleteButton addAcionBlock:^(UIButton * _Nonnull sender) {
        [bgView removeFromSuperview];
    }];
    
    [contentView createRowLineViewWithBackgroudColor:myLineColor andX:0 andY:44 andWidth:contentView.width];
    
    UILabel *contentLabel = [contentView createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:0 andTextColor:MyTitleTextColor andFont:PingFang_SC_Light14 andTextAlignment:NSTextAlignmentCenter andText:content];
    contentLabel.width = contentView.width-40;
    [contentLabel sizeToFit];
    contentLabel.y = deleteButton.maxY + 15;
    contentLabel.centerX = contentView.width/2;
    
    contentView.height = contentLabel.maxY + 30;
}

+ (void)GDTextFieldViewWithTitle:(NSString *)titleString TextField:(NSString *)textString ButtonClick:(void(^)(NSString *textString, UIView *bgView))clickBlock{
    UIView *bgView = [KWINDOW createUIViewWithBackgroudColor:[[UIColor blackColor] colorWithAlphaComponent:0.7f]];
    bgView.frame = KWINDOW.bounds;
    
    UIView *contentView = [bgView createUIViewWithBackgroudColor:[UIColor whiteColor]];
    contentView.layer.cornerRadius = 10;
    contentView.layer.masksToBounds = YES;
    contentView.size = CGSizeMake(bgView.width-94, 180);
    contentView.center = CGPointMake(bgView.width/2, KSCREEN_height/2-SafeAreaNavBarHeight/2);
    
    UILabel *titlelabel = [contentView createUILabelWithBackgroudColor:nil andNumberOfLines:2 andTextColor:MyTitleTextColor andFont:PingFang_SC_Light16 andTextAlignment:NSTextAlignmentCenter andText:titleString];
    titlelabel.width = contentView.width - 40;
    [titlelabel sizeToFit];
    titlelabel.mo_origin = CGPointMake(20, 10);
    
    UITextField *contentTextField = [contentView createUITextFieldWithBackgroudColor:UIColor.clearColor andFont:PingFang_SC_Light14 andTextColor:MyTitleTextColor andTextAlignment:NSTextAlignmentLeft andBorderStyle:UITextBorderStyleNone andPlaceholderColor:myLineColor andPlaceholder:@""];
    contentTextField.frame = CGRectMake(28, titlelabel.maxY+15, contentView.width-56, 40);
    contentTextField.clearButtonMode = UITextFieldViewModeAlways;
    contentTextField.text = textString;
    
    [contentView createRowLineViewWithBackgroudColor:myLineColor andX:0 andY:contentTextField.maxY+5 andWidth:contentView.width];
    
    CGFloat width = (contentView.width - 90) / 2.0;
    UIButton *cancelBtn = [GDRemindTool cancelButtonWithFrame:CGRectMake(30, contentTextField.maxY+25, width, 44) superView:contentView];
    [cancelBtn addAcionBlock:^(UIButton * _Nonnull sender) {
        [bgView removeFromSuperview];
    }];
    UIButton *sureButton = [GDRemindTool sureButtonButtonWithFrame:CGRectMake(cancelBtn.maxX+30, cancelBtn.y, cancelBtn.width, cancelBtn.height) superView:contentView];
    [sureButton addAcionBlock:^(UIButton * _Nonnull sender) {
        clickBlock(contentTextField.text,bgView);
    }];
    
    contentView.height = sureButton.maxY + 30;
}
//修改昵称弹窗
+ (void)GDRelationShipViewWithNickName:(NSString *)nickName ButtonClick:(void(^)(NSString *nickName))clickBlock{
    
    [GDRemindTool  GDTextFieldViewWithTitle:GDBaselocalString(@"修改设备备注名称") TextField:nickName ButtonClick:^(NSString *textString, UIView *bgView) {
        if (textString.length == 0) {
            [SVProgressHUD showError:GDBaselocalString(@"请输入昵称")];
            return;
        }
        clickBlock(textString);
        [bgView removeFromSuperview];
    }];
}
///电子围栏名称弹窗
+ (void)GDFenceViewWithName:(NSString *)fencekName ButtonClick:(void(^)(NSString *nickName))clickBlock{
    [GDRemindTool  GDTextFieldViewWithTitle:GDBaselocalString(@"保存电子围栏") TextField:fencekName ButtonClick:^(NSString *textString, UIView *bgView) {
        if (textString.length == 0) {
            [SVProgressHUD showError:GDBaselocalString(@"请输入安全区域名称")];
            return;
        }
        clickBlock(textString);
        [bgView removeFromSuperview];
    }];
}

///寻人模式切换提示框
+ (void)GDSearchModelViewButtonClick:(void(^)(BOOL confirm))clickBlock{
    
    [GDRemindTool  GDRemindToolTitle:GDBaselocalString(@"寻人模式的定位频率为5分钟/次，\n且手环在此模式中功耗增大。") subtitle:GDBaselocalString(@"确认是否切换为寻人模式") ButtonClick:^(BOOL confirm) {
        clickBlock(confirm);
    }];
}

/// 报警view type:  0:SOS警报 1:跌倒警报
+ (void)GDUserAlarmViewWithType:(NSInteger )type Content:(NSString *)contentString Adress:(NSString *)adress deviceType:(NSString *)deviceType ButtonClick:(void(^)(BOOL confirm))clickBlock{
    
    UIView *bgView = [KWINDOW createUIViewWithBackgroudColor:[[UIColor blackColor] colorWithAlphaComponent:0.7f]];
    bgView.frame = KWINDOW.bounds;
    UIView *contentView = [bgView createUIViewWithBackgroudColor:[UIColor whiteColor]];
    contentView.layer.cornerRadius = 10;
    contentView.layer.masksToBounds = YES;
    contentView.frame = CGRectMake(47, 3.2*SafeAreaNavBarHeight, bgView.width-94, 220);
    contentView.centerY = KSCREEN_height/2-SafeAreaTabbarHeight;
    
    NSString *titleString = GDBaselocalString(@"loc_SOSalarmTitle");
    if (type == 1) {titleString = GDBaselocalString(@"loc_FallAlarmTitle"); }
    UILabel *titleLabel = [contentView createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:1 andTextColor:RGBFromHex(0xE2402B) andFont:PingFang_SC_Light16 andTextAlignment:NSTextAlignmentLeft andText:titleString];
    [titleLabel sizeToFit];
    titleLabel.x = 20;
    titleLabel.centerY = 22;
    UIButton *closeButton = [contentView createUIButtonWithBackgroudColor:UIColor.clearColor andFont:nil andNormalTitleColor:nil andNormalImage:GDBaseImage(@"message_delete") andNormalBackgroundImage:nil andTitle:@""];
    closeButton.frame = CGRectMake(contentView.width-50, 0, 44, 44);
    [closeButton addAcionBlock:^(UIButton * _Nonnull sender) {
        clickBlock(NO);
        [bgView removeFromSuperview];
    }];
    [contentView createRowLineViewWithBackgroudColor:myLineColor andX:0 andY:44 andWidth:contentView.width];
    UIImageView *typeImageView = [contentView createUIImageViewWithBackgroudColor:UIColor.clearColor andContentMode:UIViewContentModeScaleToFill];
    [typeImageView setImage:GDBaseImage(@"icon_tx_sos")];
    if (type == 1) {[typeImageView setImage:GDBaseImage(@"icon_tx_dd")];}
    [typeImageView sizeToFit];
    typeImageView.centerX = contentView.width*0.5;
    typeImageView.y = 56;
    
    UILabel *remindLabel = [contentView createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:0 andTextColor:MyTitleTextColor andFont:Font16 andTextAlignment:NSTextAlignmentCenter andText:contentString];
    remindLabel.mo_origin = CGPointMake(23, typeImageView.maxY+10);
    remindLabel.width = contentView.width-46;
    MOBold(remindLabel, 19);
    [remindLabel sizeToFit];
    
    NSString *adressString = @"";
    if (adress && adress.length > 0) {
        adressString = [NSString stringWithFormat:@"%@:%@",GDBaselocalString(@"loc_adress"),adress];
    }
    UILabel *adressLabel = [contentView createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:0 andTextColor:MyTitleTextColor andFont:Font16 andTextAlignment:NSTextAlignmentLeft andText:adressString];
    adressLabel.mo_origin = CGPointMake(23, remindLabel.maxY+10);
    adressLabel.width = contentView.width-46;
    [adressLabel sizeToFit];
    
    UIButton *confirmBtn = [contentView createUIButtonWithBackgroudColor:UIColor.clearColor andFont:Font16 andNormalTitleColor:UIColor.whiteColor andNormalImage:nil andNormalBackgroundImage:GDBaseImage(@"anniu_red") andTitle:GDBaselocalString(@"详情")];
    confirmBtn.frame = CGRectMake(0, adressLabel.maxY+15, 124, 40);
    if ([deviceType isEqualToString:@"Z800"]) {
        [confirmBtn setTitle:GDBaselocalString(@"拨打电话") forState:UIControlStateNormal];
        confirmBtn.width = 180;
    }
    confirmBtn.centerX = contentView.width/2;
    [contentView addSubview:confirmBtn];
    [confirmBtn addAcionBlock:^(UIButton * _Nonnull sender) {
        clickBlock(YES);
        [bgView removeFromSuperview];
    }];
    
    contentView.height = confirmBtn.maxY +25;
}



+ (UIButton *)cancelButtonWithFrame:(CGRect)frame superView:(UIView *)superView{
    UIImage *btnImg = GDBaseImage(@"message_delete");
    if ([AccountModel GetMainTypeOrange]) {
        btnImg = GDBaseImage(@"icon_nosave_orange");
    }
    UIButton *cancelButton = [superView createUIButtonWithBackgroudColor:[UIColor whiteColor] andFont:PingFang_SC_Light15 andNormalTitleColor:MainColor andNormalImage:btnImg andNormalBackgroundImage:nil andTitle:GDBaselocalString(@"取消") ];
    cancelButton.frame = frame;
    [cancelButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
    cancelButton.layer.cornerRadius =  22;
    cancelButton.layer.borderWidth = 1;
    cancelButton.layer.borderColor = MainColor.CGColor;
    cancelButton.layer.masksToBounds = YES;
    return cancelButton;
}
+ (UIButton *)sureButtonButtonWithFrame:(CGRect)frame superView:(UIView *)superView{
    UIButton *sureButton = [superView createUIButtonWithBackgroudColor:MainColor andFont:PingFang_SC_Light15 andNormalTitleColor:UIColor.whiteColor andNormalImage:GDBaseImage(@"icon_save_orange") andNormalBackgroundImage:nil andTitle:GDBaselocalString(@"确定") ];
    sureButton.frame = frame;
    [sureButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
    sureButton.layer.cornerRadius =  22;
    sureButton.layer.borderWidth = 1;
    sureButton.layer.borderColor = MainColor.CGColor;
    sureButton.layer.masksToBounds = YES;
    return sureButton;
}

@end
